﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using SAASInvest.Contracts;
using System.IO;
using System.Web;

namespace SAASInvest.ServersHost
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "AsynFileUpload" in code, svc and config file together.
    public class AsynFileUpload : IAsynFileUpload
    {
        public FileUploadData AnysUploadFile(FileUploadData file)
        {
            string filePath = System.Configuration.ConfigurationManager.AppSettings["filePath"] + "\\" + file.FileName;
            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                long offset = file.Offset;
                BinaryWriter writer = new BinaryWriter(fs);
                writer.Seek((int)offset, SeekOrigin.Begin);
                writer.Write(file.UploadData);

                file.Offset = fs.Length;
                file.UploadData = null;
                writer.Close();
                fs.Close();
            }

            return file;
        }


        public List<FileUploadData> GetFileDatasList()
        {
            return new List<FileUploadData>();
        }

        public FileUploadData GetFile(string fileFullName)
        {
            string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, @"Upload\" + fileFullName);            
            if (File.Exists(filePath))
            {
                FileUploadData uploadData = new FileUploadData()
                {
                    FileName = fileFullName
                };
                using (FileStream stream = new FileStream(filePath, FileMode.Open))
                {
                    long length = stream.Length;
                    uploadData.Offset = length;
                    stream.Close();                     
                }
                return uploadData;
            }
            return null;
        }
    }
}
